const Article = require('../model/article');
const Tab = require('../model/tab');
/**
 * 文章中间件
 * 
 */
module.exports = {
    /**
     * 获取热门文章
     */
    getHot:(req,res,next) =>{
        Article.getHot(3).then(results =>{
            req.hots = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取最新文章
     */
    getList:(req,res,next) =>{
        Article.getList().then(results =>{
            req.article = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定目录下的文章列表
     */
    getListCategoryId:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getListCategoryId(id).then(results =>{
            req.articles = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定关键词的文章列表
     */
    getListBykeywrod:(req,res,next) =>{
        // 获取搜索框的表单数据
        let keyword = req.query.keyword;
        console.log(keyword);
        Article.getListBykeywrod(keyword).then(results =>{
            req.articles = results;
            next();
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定文章的详情
    */
   getArticleById:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getArticleById(id).then(results =>{
            req.articles = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取指定文章的标签列表
    */
   getTabs:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Tab.getListByArticleId(id).then(results =>{
            req.tabs = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取上一篇文章
    */
    getPrevArticle:(req,res,next) =>{
        // 从路由中传递参数进来
        let id = req.params.id;
        Article.getPrevArticle(id).then(results =>{
            req.prev = results;
            next()
        }).catch(err=>{
            next(err);
        });
    },
    /**
     * 获取下一篇文章
    */
   getNextArticle:(req,res,next) =>{
    // 从路由中传递参数进来
    let id = req.params.id;
    Article.getNextArticle(id).then(results =>{
        req.next = results;
        next()
    }).catch(err=>{
        next(err);
    });
},
}